package com.thinkgem.jeesite.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.thinkgem.jeesite.modules.sys.utils.UserUtils;


/**
 * 基础平台邮件发送器
 * 
 * @author wanxiaobo
 */
public class BdpMailSender {
  
  /**
   * 注入Spring的邮件发送器
   */
  private JavaMailSender javaMailSender;
  
  /**
   * 给用户自己发送邮件
   * 
   * @throws MessagingException
   *         邮件发送异常
   * @param emailSubject
   *        邮件主题
   * @param emailContent
   *        邮件内容
   */
  public void sendEmailToUserself(String emailSubject, String emailContent)
      throws MessagingException {
    System.out.println(javaMailSender);
    MimeMessage message = javaMailSender.createMimeMessage();
    try {
      MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
      
      helper.setFrom(getMailHostUsername());
      helper.setTo(UserUtils.getUser().getName());
      helper.setSubject(emailSubject);
      helper.setText(emailContent);
      javaMailSender.send(message);
    }
    catch (MessagingException e) {
      System.out.println("邮件发送失败");
      e.printStackTrace();
      throw e;
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
  
  /**
   * 该方法用来给指定的邮箱发送邮件
   * 
   * @param specifyEmailAddress
   *        指定邮箱地址
   * @param emailSubject
   *        邮件主题
   * @param emailContent
   *        邮件内容
   * @throws MessagingException
   *         邮件发送失败异常
   */
  public void sendEmailTo(String specifyEmailAddress, String emailSubject, String emailContent)
      throws MessagingException {
    System.out.println(javaMailSender);
    MimeMessage message = javaMailSender.createMimeMessage();
    try {
      MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
      
      helper.setFrom(getMailHostUsername());
      helper.setTo(specifyEmailAddress);
      helper.setSubject(emailSubject);
      helper.setText(emailContent, true);
      javaMailSender.send(message);
    }
    catch (MessagingException e) {
      System.out.println("邮件发送失败");
      e.printStackTrace();
      throw e;
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
  /**
   * 该方法用来给指定的多个邮箱发送邮件
   * 
   * @param addresss
   *        指定邮箱地址
   * @param ccAddresss
   *        指定抄送邮箱地址     
   * @param emailSubject
   *        邮件主题
   * @param emailContent
   *        邮件内容
   * @throws MessagingException
   *         邮件发送失败异常
   */
  public void sendEmailToMuti(Address[] addresss, Address[] ccAddresss, String emailSubject, String emailContent)
      throws MessagingException {
    MimeMessage message = javaMailSender.createMimeMessage();
    message.setRecipients(Message.RecipientType.TO, addresss);
    message.setRecipients(Message.RecipientType.CC, ccAddresss);
    try {
      MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
      String nick = javax.mail.internet.MimeUtility.encodeText("房天下客服", "UTF-8", "B");  
      helper.setFrom(new InternetAddress(nick + " <" + getMailHostUsername() + ">"));  
      helper.setSubject(emailSubject);
      helper.setText(emailContent, true);
      javaMailSender.send(message);
    }
    catch (MessagingException e) {
      System.out.println("邮件发送失败");
      e.printStackTrace();
      throw e;
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
  /**
   * 获取服务器邮箱用户名信息
   * 
   * @return String 服务器邮箱用户名 该用户名在mail.properties里有配置
   * @throws IOException
   *         读取文件异常
   */
  public static String getMailHostUsername() throws IOException {
    InputStream inputStream = BdpMailSender.class.getClassLoader().getResourceAsStream(
        "mail.properties");
    Properties config = new Properties();
    config.load(inputStream);
    return config.getProperty("mail.username");
  }
  
  /**
   * @param javaMailSender
   *        set javaMailSender
   */
  public void setJavaMailSender(JavaMailSender javaMailSender) {
    this.javaMailSender = javaMailSender;
  }
}
